package com.yuncool.auth.utils;

import com.yuncool.auth.bo.UserDetailsBO;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

/**
 * 获取Session中的用户
 */
public class SessionUser {

    /**
     * 获取用户详情
     * @return
     */
    public UserDetailsBO getUserDetail() {
        return (UserDetailsBO) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
    }

    /**
     * 获取用户登陆情况(请勿在消息队列中使用)
     * @return true:已登陆 false:未登陆
     */
    public Boolean checkUserStatus(){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null){
            return false;
        }
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            return true;
        }
        return false;
    }

}
